package com.hjm.java.service;

import android.content.Context;
import android.os.Handler;
import android.util.Log;

import com.hjm.java.entity.GoodsListVO;
import com.hjm.java.entity.ShopCartVO;
import com.hjm.java.entity.StoreNavListVO;
import com.hjm.java.sql.HJMHelper;
import com.hjm.java.utils.ClassCopyUtil;
import com.hjm.java.utils.HjmSuccessCallBack;
import com.hjm.java.utils.HjmFailCallBack;
import com.hjm.java.utils.HjmGlideUtil;
import com.hjm.java.utils.HjmListCallBack;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainService extends BaseService{
    private static final String TAG = "MainService";
    private List<GoodsListVO> goodsListVOList;
    private List<ShopCartVO> shopCartVOList;
    private HjmListCallBack<StoreNavListVO> storeNavListVOHjmListCallBack;

    public MainService(Context context, Handler mHandler) {
        super(context, mHandler);
    }

    /**
     * 获取商品列表
     *
     * @param num      随机商品数量
     * @param callBack 回调
     */
    public void getGoodsList(int num, HjmSuccessCallBack callBack) {
        httpUtils.getList(baseUrl + "/goods/random?num=" + num, GoodsListVO.class, (isSuccess, t, message) -> {
            if (isSuccess) {
                Log.d(TAG, "getGoodsList: 获取商品数据成功，开始获取商品图片 " + t.size());
                HjmGlideUtil.UrlToBitmap(mContext, baseImgUrl,
                        t.stream().map(GoodsListVO -> String.valueOf(GoodsListVO.getIcon())).toArray(String[]::new),
                        HJMHelper.getInstance(mContext),
                        (position, bitmap) -> {
                            Log.d(TAG, "getGoodsList: 商品图片获取成功，位置 " + position);
                            t.get(position).setIconBitmap(bitmap);
                        },
                        () -> {
                            Log.d(TAG, "getGoodsList: 全部商品图片获取成功");
                            this.goodsListVOList = t;
                            mHandler.post(() -> {
                                callBack.onSuccess();
                            });
                        });
            } else {
                Log.d(TAG, "getGoodsList: 获取失败 " + message);
            }
        });
    }

    /**
     * 更新购物车信息
     *
     * @param orderId         订单id（每个购物车项都是订单）
     * @param num             需要改变的商品数量
     * @param successCallBack 成功回调
     * @param failCallBack    失败回调
     */
    public void updateShopCartItem(int orderId, int num, HjmSuccessCallBack successCallBack, HjmFailCallBack failCallBack) {
        Map<String, String> map = new HashMap<>();
        map.put("id", String.valueOf(orderId));
        map.put("goodsNum", String.valueOf(num));
        httpUtils.putByToken(baseUrl + "/order", map, String.class, (isSuccess, string, msg) -> {
            mHandler.post(() -> {
                if (isSuccess) {
                    if (successCallBack != null)
                        successCallBack.onSuccess();
                } else {
                    if (failCallBack != null)
                        failCallBack.onFail(msg);
                }
            });
        });
    }

    /**
     * 删除购物车项
     *
     * @param orderId      订单id
     * @param callBack     回调
     * @param failCallBack 失败回调
     */
    public void deleteShopCartItem(int orderId, HjmSuccessCallBack callBack, HjmFailCallBack failCallBack) {
        httpUtils.deleteByToken(baseUrl + "/order/?id=" + orderId, String.class, (isSuccess, string, msg) -> {
            mHandler.post(() -> {
                if (isSuccess) {
                    callBack.onSuccess();
                } else {
                    failCallBack.onFail(msg);
                }
            });
        });
    }

    public void getShopCartData(HjmListCallBack<com.hjm.ui.shopcart.entity.ShopCartVO> listCallBack) {
        httpUtils.getListByToken(baseUrl + "/order/sc", ShopCartVO.class, (isSuccess, t, message) -> {
            if (isSuccess) {
                List<com.hjm.ui.shopcart.entity.ShopCartVO> shopCartVOS = new ArrayList<>();
                HjmGlideUtil.UrlToBitmap(mContext, baseImgUrl,
                        t.stream().map(ShopCartVO -> String.valueOf(ShopCartVO.getGoodsIconId())).toArray(String[]::new),
                        HJMHelper.getInstance(mContext),
                        (position, bitmap) -> {
                            Log.d(TAG, "getShopCartList: 商品图片获取成功，位置 " + position);
                            t.get(position).setGoodsIconBitmap(bitmap);
                            // 将商品信息复制一份到新的集合中
                            com.hjm.ui.shopcart.entity.ShopCartVO shopCartVO = new com.hjm.ui.shopcart.entity.ShopCartVO();
                            ClassCopyUtil.copyProperties(t.get(position), shopCartVO);
                            shopCartVOS.add(shopCartVO);
                        },
                        () -> {
                            Log.d(TAG, "getShopCartList: 全部商品图片获取成功");
                            mHandler.post(() -> {
                                this.shopCartVOList = t;
                                listCallBack.call(true, shopCartVOS, null);
                            });
                        });
            } else {
                mHandler.post(() -> {
                    Log.e(TAG, "getShopCartList: 获取失败 " + message);
                    listCallBack.call(false, null, message);
                });
            }
        });
    }

    /**
     * 获取商店导航列表
     * @param callBack
     */
    public void getStoreNavListVO(HjmListCallBack<StoreNavListVO> callBack){
        storeNavListVOHjmListCallBack = (isSuccess, t, msg) -> {
            if (isSuccess) {
                callBack.call(true, t, null);
                HjmGlideUtil.UrlToBitmap(mContext, baseImgUrl,
                        t.stream().map(StoreNavListVO -> String.valueOf(StoreNavListVO.getIcon())).toArray(String[]::new),
                        HJMHelper.getInstance(mContext),
                        (position, bitmap) -> {
                            t.get(position).setIconBitmap(bitmap);
                        },
                        () -> {
                            mHandler.post(() -> {
                                callBack.call(true, t, null);
                            });
                        });
            } else {
                mHandler.post(() -> {
                    callBack.call(false, null, msg);
                });
            }
        };
        httpUtils.getList(baseUrl + "/store/list", StoreNavListVO.class, storeNavListVOHjmListCallBack);
    }

    public List<GoodsListVO> getGoodsListVOList() {
        return goodsListVOList;
    }

    public List<ShopCartVO> getShopCartVOList() {
        return shopCartVOList;
    }
}
